package com.silverhaze.diamond.client.settings;

import com.silverhaze.diamond.common.entity.SystemUser;
import com.silverhaze.commons.utils.settings.JAXBUtils;
import com.silverhaze.diamond.client.settings.ui.MainFrameSettings;
import com.silverhaze.diamond.client.settings.ui.UISettings;
import com.silverhaze.diamond.client.settings.ui.customers.CustomersTabSettings;
import com.silverhaze.diamond.client.settings.ui.details.DetailsTabSettings;
import com.silverhaze.diamond.client.settings.ui.service.ServicePanelSettings;
import com.silverhaze.diamond.client.settings.ui.service.ServiceTabSettings;

import java.io.File;
import java.util.Date;

/**
 * Created by Ivan Babanin (23:26:46 30.05.2008)
 * This part of code is strictly confidential.
 * © Magnificent Software Inc.
 */
public class SettingsStorage {
    private static String SETTINGS_FOLDER_PATH = "data";
    private static String SETTINGS_PATH = SETTINGS_FOLDER_PATH + "/settings.xml";

    private static Settings settings;
    private static UserSettings userSettings;

    private static Class[] classes = new Class[]{
            Settings.class, UISettings.class,
            ServicePanelSettings.class, ServiceTabSettings.class,
            CustomersTabSettings.class, DetailsTabSettings.class
    };

    public static Settings getSettings() {
        if (settings == null) {
            File settingsFolder = new File(SETTINGS_FOLDER_PATH);
            if(!settingsFolder.exists()){
                if(!settingsFolder.mkdir()){
                    //...
                }
            }


            if (new File(SETTINGS_PATH).exists()) {
                settings = (Settings) JAXBUtils.loadSettings(SETTINGS_PATH, classes);
            } else {
                settings = initializeSettings();
            }
        }

        return settings;
    }

    public static void saveSettings() {
        if (settings == null) {
            settings = initializeSettings();
        }

        JAXBUtils.saveSettings(SETTINGS_PATH, classes, settings);
    }

    private static Settings initializeSettings() {
        Settings settings = new Settings();
        UISettings uiSettings = new UISettings();
        ServiceTabSettings serviceTabSettings = new ServiceTabSettings();
        ServicePanelSettings servicePanelSettings = new ServicePanelSettings();
        MainFrameSettings mainFrameSettings = new MainFrameSettings();

        servicePanelSettings.setFrom(new Date());
        servicePanelSettings.setTill(new Date());

        serviceTabSettings.setServicePanelSettings(servicePanelSettings);
        uiSettings.setServiceTabSettings(serviceTabSettings);
        uiSettings.setMainFrameSettings(mainFrameSettings);
        settings.setUiSettings(uiSettings);

        return settings;
    }

    public static UserSettings getUserSettings() {
        if (userSettings == null) {
            userSettings = intializeUserSettings();
        }

        return userSettings;
    }

    private static UserSettings intializeUserSettings() {
        UserSettings userSettings = new UserSettings();
        SystemUser user = new SystemUser();

        userSettings.setCurrentUser(user);

        return userSettings;
    }
}

